var express = require('express');
var router = express.Router();
var session = require('express-session');

const MerchantService = require('../server/service/merchant.service.js');
//获取当前用户
router.get('/currentUser', function (req, res, next) {
    if (req.session.user) {
        res.send({ status: true, data: req.session.user });
    } else {
        res.send({ status: false, data: {} });
    }
});

//商家列表
router.get('/api', function (req, res, next) {
    MerchantService.getMerchantList(function (doc) {
        if (doc.status == true) {
            if (doc.data === {}) {
                doc.message = '暂无数据';
            }
        }
        console.log('[C--请求商家列表]' + doc);
        res.send(doc);
    });


});
//根据id获取商家信息
router.get('/api/:_id', function (req, res, next) {
    MerchantService.getMerchantById(req.params._id, function (doc) {
        if (doc.status == true) {
            if (doc.data === {}) {
                doc.message = '暂无数据';
            }
        }
        console.log('[C--请求商家信息]' + doc);
        res.send(doc);
    });


});
// 根据foodType的Id获取所有商家的信息
router.get('/getbyfoodtype/:_id', function (req, res, next) {
    console.log(req.params._id);
    MerchantService.getMerchantListByFoodtype(req.params._id, function (doc) {
        if (doc.status == true) {
            if (doc.data === {}) {
                doc.message = '暂无数据';
            }
        }
        console.log('[C--请求商家信息ByFoodtype]' + doc);
        res.send(doc);
    });
});
//删除商家信息
router.delete('/:_id', function (req, res, next) {
    MerchantService.removeMerchantById(req.params._id, function (doc) {
        res.send(doc);
    });

});
//修改商家信息
router.put('/:_id', function (req, res, next) {
    let updateData = {
        _id: req.body.id,
        merchantName: req.body.name,
        password: req.body.password,
        address: req.body.address,
        headPic: req.body.headPic,
        idCardPic: req.body.idCardPic
    };
    MerchantService.updateMerchant(updateData, function (doc) {
        res.send(doc);
    });

});

module.exports = router;